﻿/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       BcBrand
- 机器名称：       YTE
- 命名空间：       LD.BC
- 文 件 名：       BcBrand
- 创建时间：       2022/6/29 19:33:44
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace LD.BC
{
    public class BcBrand
    {
        public List<TB_BcBrand> GetList()
        {
            List<TB_BcBrand> bcs = new List<TB_BcBrand>();

            var c_class = GetClasses();
            foreach (var item in c_class)
            {
                BcAttribute bcAttribute = (BcAttribute)item.GetCustomAttribute(typeof(BcAttribute), true);
                if (bcAttribute != null)
                {
                    bcs.Add(new TB_BcBrand()
                    {
                        ClassName = item.Name,
                        Brand = bcAttribute.Brand,
                        Param = bcAttribute.Param,
                        Remark = bcAttribute.Remark,
                    });
                }
            }
            return bcs;
        }
        private IEnumerable<Type> GetClasses()
        {
            //反射
            var typess = Assembly.GetCallingAssembly().GetTypes().Where(p => p.GetInterfaces().Contains(typeof(IBoardCard))).ToList();
            return typess;
        }
        public IBoardCard GetBcInstance(string className)
        {
            IBoardCard bc = null;
            //反射
            Type type = Type.GetType($"LD.BC.{className}");
            if (type != null)
                bc = (IBoardCard)type.Assembly.CreateInstance($"LD.BC.{className}");
            //var typess = Assembly.GetCallingAssembly().GetTypes()
            //    .Where(p => p.BaseType == typeof(IBoardCard));
            //var assembly = Assembly.LoadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LD.BC.dll"));
            //var bc = (IBoardCard)assembly.CreateInstance($"LD.BC.{ className}", true);
            return bc;
        }
        public IBoardCard GetBcInstance(BrandType brandType)
        {
            IBoardCard bc = null;
            var instance = GetList().Where(p => p.Brand == brandType).FirstOrDefault();
            if (instance == null) return null;
            //反射
            Type type = Type.GetType($"LD.BC.{instance.ClassName}");
            if (type != null)
                bc = (IBoardCard)type.Assembly.CreateInstance($"LD.BC.{instance.ClassName}");
            //var typess = Assembly.GetCallingAssembly().GetTypes()
            //    .Where(p => p.BaseType == typeof(IBoardCard));
            //var assembly = Assembly.LoadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "LD.BC.dll"));
            //var bc = (IBoardCard)assembly.CreateInstance($"LD.BC.{ className}", true);
            return bc;
        }
    }
}
